package com.iedu.demo.lgq.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.iedu.demo.lgq.entity.TbUser;
import com.iedu.demo.lgq.entity.TbUserAddress;
import com.iedu.demo.lgq.mapper.TbUserAddressMapper;
import com.iedu.demo.lgq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iedu.demo.lgq.mapper.UserMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private TbUserAddressMapper tbUserAddressMapper;

    @Override
    public TbUser login(String userName, String password) {
        // 使用 SaToken 的加密工具加密密码
        String encryptedPwd = SaSecureUtil.md5(password); // 使用 SaToken 提供的 MD5 加密
        TbUser user = mapper.login(userName, encryptedPwd);
        return user;
    }

    @Override
    public void addUser(TbUser sysUser) {
//        if (newUser.getUserName() != null && newUser.getPassword() != null) {
//            // 使用 SaToken 的加密工具来加密密码
//            newUser.setPassword(SaSecureUtil.md5(newUser.getPassword())); // 使用 MD5 加密密码
//
//            // 自动设置 addDate 为当前日期
//            newUser.setAddDate(new Date(System.currentTimeMillis())); // 当前时间
//
//            // 保存用户
//            mapper.saveUser(newUser);
//        }
        if(sysUser.getUserName() != null && !sysUser.getUserName().trim().isEmpty()){
            List<TbUser> userList = mapper.selectByUserName(sysUser.getUserName());
            if(userList.size()==0) {
                sysUser.setPassword(SaSecureUtil.md5(sysUser.getPassword()));
                mapper.saveUser(sysUser);
            }
        }
    }

    @Override
    public List<TbUser> findAll() {
        return mapper.findAll();
    }

    @Override
    public List<TbUser> findByWhere(TbUser sysUser) {
        return mapper.findByWhere(sysUser);
    }

    // 更新用户状态
    @Override
    public boolean updateUserStatus(String userId, int state) {
        int rowsAffected = mapper.updateUserStatus(userId, state);
        return rowsAffected > 0;
    }

    // 更新用户头像
    @Override
    public boolean updateUserAvatar(String userId, String avatarUrl) {
        int rowsAffected = mapper.updateUserAvatar(userId, avatarUrl);
        return rowsAffected > 0;
    }

    // 验证邮箱
    @Override
    public boolean verifyEmail(String userId, String email) {
        TbUser user = mapper.findById(userId);
        if (user != null && user.getEmail().equals(email)) {
            return true;
        }
        return false;
    }
    @Override
    public boolean updateUserDetails(TbUser user) {
        // 如果没有提供 id，则返回 false
        if (user.getId() == null) {
            return false;
        }
        int rowsAffected = mapper.updateUserDetails(user);
        return rowsAffected > 0;
    }

    @Transactional
    @Override
    public boolean addUserAddress(String userName, String regionId, String cityId, String address, String tel, String realName) {

        // 创建一个 TbUserAddress 对象，并填充用户地址信息
        TbUserAddress userAddress = new TbUserAddress();
        userAddress.setUserName(userName);  // 设置用户姓名
        userAddress.setRegionId(regionId);
        userAddress.setCityId(cityId);
        userAddress.setAddress(address);
        userAddress.setTel(tel);
        userAddress.setRealName(realName);
        System.out.println("准备被插入的数据："+userAddress.toString());
        try {
            // 保存收货地址到数据库
            System.out.println("执行插入语句");
            System.out.println("********************************");
            tbUserAddressMapper.insert(userAddress);  // 调用 insert 方法保存地址
            System.out.println("插入成功**********************************");
            // 如果没有抛出异常，则表示插入成功，返回 true
            return true;
        } catch (Exception e) {
            // 发生异常时，返回 false
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TbUser findById(String userName) {
        return mapper.findById(userName);
    }
    @Override
    public List<TbUserAddress> getUserAddresses(String userName) {
        // 根据用户名获取用户ID
        TbUser user = mapper.findById(userName);
        if (user != null) {
            return tbUserAddressMapper.findByUserName(user.getUserName());  // 假设你有 findByUserId 方法
        }
        return null;
    }

    @Override
    public TbUserAddress getUserAddressById(Long addressId) {
        return tbUserAddressMapper.findById(addressId);  // 根据地址ID查询收货地址
    }

}
